/*
 * Copyright 2011 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.db_vdumper.ui.component.sql;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author yura
 */
public class RowHeaderRenderer extends JLabel implements TableCellRenderer {

    JTable reftable;

    public RowHeaderRenderer(JTable reftable) {
        this.reftable = reftable;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table,
            Object obj,
            boolean isSelected,
            boolean hasFocus,
            int row, int col) {
        ((DefaultTableModel) table.getModel()).setRowCount(reftable.getRowCount());
        JTableHeader header = reftable.getTableHeader();
        this.setOpaque(true);
        // Set the border TableHeader type
        setBorder(UIManager.getBorder("TableHeader.cellBorder"));
        setHorizontalAlignment(CENTER);
        // Set the background color for the background color TableHeader
        setBackground(header.getBackground());
        Font font = header.getFont();
        // When selecting a cell in the row header set to select the color
        if (isSelect(row)) {
            setForeground(Color.white);
            setBackground(Color.lightGray);
            setFont(font);
            setText(String.valueOf(row + 1));
        } else {
            setForeground(header.getForeground());
            setFont(font);
            setText(String.valueOf(row + 1));
        }
        return this;
    }

    private boolean isSelect(int row) {
        int[] sel = reftable.getSelectedRows();
        for (int i = 0; i < sel.length; i++) {
            if (sel[i] == row) {
                return true;
            }
        }
        return false;
    }
}
